package dk.dashboardFP.panels.ui.text.label {
	import flash.display.Sprite;
	import flash.events.FocusEvent;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.text.AntiAliasType;
	import flash.text.Font;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;

	/**
	 * @author jk
	 */
	public class AbstractLabel extends Sprite {
		private var _sText : String;
		private var _nSize : Number;
		private var _bBold : Boolean;
		private var _sFont : String;
		protected var _tfLabel : TextField;
		private var _uintColor : uint;
		private var _nWidth : Number;
		private var _nHeight : Number;
		private var _bHtml : Boolean = false;
		private var _nKerning : Number;
		private var _bKerningEnabled : Boolean;
		private var _sAlign : String;
		private var _bUnderlined : Boolean = false;
		private var _bSelectable : Boolean;
		private var _bSelectAllOnFocus : Boolean = false;
		private var _bHasEmbeddedFont : Boolean = true;
		private var _nLeading : Number = NaN;
		private var _fClickCallback : Function;
		private var _sprMaskedPlane : Sprite;

		
		public function AbstractLabel(sText : String = "LabelText", sSize : Number = 12, uintColor : uint = 0x000000, bBold : Boolean = false, sFont : String = "", embeddedFont:Boolean=true) {
			
			_bHasEmbeddedFont = embeddedFont;
			_sFont = sFont;
			_uintColor = uintColor;
			_bBold = bBold;
			_nSize = sSize;
			_sText = sText;
			
			if (_sFont == "") {
				
				if (Font.enumerateFonts().length > 0) {
					_sFont = Font(Font.enumerateFonts()[0]).fontName;
				} else {
					_bHasEmbeddedFont = false;
				}
			} 
			
			setup();
			update();			
		}

		private function setup() : void {
			
			_tfLabel = new TextField();
			addChild(_tfLabel);
			
			//_tfLabel.addEventListener(FocusEvent.FOCUS_IN, onTFFocusIn, false, 0, true);
			//_tfLabel.addEventListener(FocusEvent.FOCUS_OUT, onTFFocusOut, false, 0, true);
			
			_tfLabel.autoSize = TextFieldAutoSize.LEFT;
			_tfLabel.embedFonts = _bHasEmbeddedFont;
			
			_tfLabel.selectable = false;
			
			_tfLabel.antiAliasType = AntiAliasType.ADVANCED;
			
			uintColor = _uintColor;
		}

		private function onTFFocusOut(event : FocusEvent) : void {
		}

		private function onTFFocusIn(event : FocusEvent) : void {
			if (_bSelectAllOnFocus) {
			}
		}

		private function selectAll() : void {
			_tfLabel.setSelection(0, _tfLabel.text.length);
		}

		private function update() : void {
			
			if (_bHtml) {
				_tfLabel.htmlText = _sText;
			} else {
				
				var tforNew : TextFormat = new TextFormat(sFont, nSize, uintColor, bBold);
				tforNew.kerning = _bKerningEnabled;
				
				tforNew.underline = _bUnderlined;
				
				if(_bBold) {
					tforNew.bold = true;
				}
				
				if (!isNaN(_nLeading)) {
					tforNew.leading = _nLeading;
				}
				
				if (_bKerningEnabled) {
					tforNew.letterSpacing = _nKerning;
				}
				
				if (_sAlign) {
					tforNew.align = _sAlign;
				}
				
				
				_tfLabel.defaultTextFormat = tforNew;
				_tfLabel.text = sText;
				
				if (_sAlign == TextFormatAlign.RIGHT) {
					_tfLabel.x = -_tfLabel.width;
				}
			}
			
			dispatchEvent(new LabelEvent(LabelEvent.UPDATED));
		}

		public static function composeHtmlString(sText : String, sFont : String, nSize : Number, uintColor : uint, bBold : Boolean) : String {
			return "<font face='" + sFont + "' size='" + nSize + "' color='#" + uintColor.toString(16).toUpperCase() + "'>" + sText + "</font>";
		}

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// alingment handling
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

		private function setAlignMent() : void {
			update();
			
			switch(_sAlign) {
				case TextFormatAlign.LEFT:
					_tfLabel.x = 0;
					break;
				case TextFormatAlign.RIGHT:
					_tfLabel.x = -_tfLabel.width;
					break;
				case TextFormatAlign.CENTER:
					_tfLabel.x = -_tfLabel.width / 2;
					break;
			}
		}

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// add and mask plane
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

		public function addAndMaskPlane(sprPlane : Sprite, bFitToLabelSize : Boolean = true) : void {
			
			_sprMaskedPlane = sprPlane;
			addChild(_sprMaskedPlane);
			
			if (bFitToLabelSize) {
				_sprMaskedPlane.width = nWidth;
				_sprMaskedPlane.height = nHeight;
			}
			_sprMaskedPlane.mask = _tfLabel;
		}

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// size
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

		private function updateSize() : void {
			
			if (_nHeight) {
				_tfLabel.autoSize = TextFieldAutoSize.NONE;
			}
			
			if (_nWidth) {
				if (_tfLabel.multiline) {
					_tfLabel.autoSize = TextFieldAutoSize.LEFT;
				} else {
					_tfLabel.autoSize = TextFieldAutoSize.NONE;
				}
				_tfLabel.width = _nWidth;
			}
			
			if (_nHeight) {
				if (this is InputLabel) {
				}
				_tfLabel.autoSize = TextFieldAutoSize.NONE;
				_tfLabel.height = _nHeight;
			}
		}

		
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// make button
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

		public function makeButton(fClickCallback : Function = null) : void {
			
			if(fClickCallback != null) {
				_fClickCallback = fClickCallback;
				addEventListener(MouseEvent.CLICK, onMouseClicked, false, 0, true);
			}
			buttonMode = true;
			mouseChildren = false;
			mouseEnabled = true;
		}

		private function onMouseClicked(event : MouseEvent) : void {
			if (_fClickCallback != null) {
				_fClickCallback();
			}
		}

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// make multiline
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

		public function makeMultiline() : void {
			_tfLabel.multiline = true;
			_tfLabel.wordWrap = true;
			updateSize();
			update();
		}
		public function makeSingleLine() : void {
			_tfLabel.multiline = false;
			_tfLabel.wordWrap = false;
			updateSize();
			update();
		}

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// getter setters
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

		public function get sAlign() : String { 
			return _sAlign; 
		}

		public function set sAlign(oNewValue : String) : void { 
			_sAlign = oNewValue;
			setAlignMent();
		}

		public function get sText() : String {
			return _sText;
		}

		public function set sText(sText : String) : void {
			_bHtml = false;
			_sText = sText;
			update();
		}

		public function get sHtmlText() : String { 
			return _sText; 
		}

		public function set sHtmlText(oNewValue : String) : void {
			_bHtml = true; 
			_sText = oNewValue;
			update();
		}

		public function get uintColor() : uint { 
			return _uintColor; 
		}

		public function set uintColor(oNewValue : uint) : void { 
			_uintColor = oNewValue;
			update();
		}

		public function get nSize() : Number { 
			return _nSize; 
		}

		public function set nSize(oNewValue : Number) : void { 
			_nSize = oNewValue;
			update();
		}

		public function get sFont() : String { 
			return _sFont; 
		}

		public function set sFont(oNewValue : String) : void { 
			_sFont = oNewValue;
			update();
		}

		public function get nWidth() : Number { 
			if (!isNaN(_nWidth)) {
				return _nWidth; 
			} else {
				return _tfLabel.width;
			}
		}

		public function set nWidth(oNewValue : Number) : void { 
			_nWidth = oNewValue;
			updateSize();
		}

		public function get nHeight() : Number {
			if (!isNaN(_nHeight)) {
				return _nHeight; 
			} else {
				return _tfLabel.height;
			}
		}

		public function set nHeight(nHeight : Number) : void {
			_nHeight = nHeight;
			updateSize();
		}

		public function get tfLabel() : TextField {
			return _tfLabel;
		}

		public function get bBold() : Boolean {
			return _bBold;
		}

		public function set bBold(bBold : Boolean) : void {
			_bBold = bBold;
		}

		public function get nKerning() : Number {
			return _nKerning; 
		}

		public function set nKerning(oNewValue : Number) : void { 
			_bKerningEnabled = true; 
			_nKerning = oNewValue;
			update();
		}

		public function get bUnderlined() : Boolean {
			return _bUnderlined;
		}

		public function set bUnderlined(bUnderlined : Boolean) : void {
			_bUnderlined = bUnderlined;
			update();
		}

		public function get bSelectable() : Boolean { 
			return _bSelectable; 
		}

		public function set bSelectable(oNewValue : Boolean) : void {
			_tfLabel.selectable = oNewValue; 
			_bSelectable = oNewValue;
		}

		public function get bSelectAllOnFocus() : Boolean {
			return _bSelectAllOnFocus;
		}

		public function set bSelectAllOnFocus(bSelectAllOnFocus : Boolean) : void {
			_bSelectAllOnFocus = bSelectAllOnFocus;
		}

		public function get nLeading() : Number { 
			return _nLeading; 
		}

		public function set nLeading(oNewValue : Number) : void { 
			_nLeading = oNewValue;
			update();
		}
	}
}
